package com.kingsoft.miot.bridge.controller;

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kingsoft.miot.bridge.pojo.common.StatisticsBean;
import com.kingsoft.miot.bridge.pojo.common.StatisticsData;
import com.kingsoft.miot.bridge.pojo.common.Tooltips;
import com.kingsoft.miot.bridge.pojo.hotel.HotelCustomerInfo;
import com.kingsoft.miot.bridge.pojo.log.LogBean;
import com.kingsoft.miot.bridge.pojo.log.LogData;
import com.kingsoft.miot.bridge.utils.Paging;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSON;
import com.kingsoft.miot.bridge.common.CommonResult;
import com.kingsoft.miot.bridge.controller.req.HotelCustomerReq;
import com.kingsoft.miot.bridge.controller.resp.HotelCustomerBrief;
import com.kingsoft.miot.bridge.controller.resp.HotelCustomerResp;
import com.kingsoft.miot.bridge.utils.ValidationUtils;

/**
 * 酒店消费者
 *
 * @author melody
 */
@RestController
public class HotelCustomerController {

    private static final Logger LOGGER = LoggerFactory.getLogger(HotelCustomerController.class);

//	@Autowired
//	private HotelCustomerService hotelCustomerService;

//    /**
//     * 查看酒店用户列表
//     *
//     * @param req
//     * @return
//     */
//    @RequestMapping(value = "/data/users", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
//    public String getHotelCustomer(@RequestBody HotelCustomerReq req) {
//        try {
//            ValidationUtils.validate(req);
//        } catch (Exception e) {
//            LOGGER.error("获取用户数据失败。");
//            return CommonResult.returnJson(false, 400, "数据校验失败。");
//        }
//        // 返回伪造数据
//        HotelCustomerResp resp = new HotelCustomerResp();
//
//        resp.setTotal_pages(11);
//        resp.setSize(10);
//        resp.setTotal_count(110);
//        resp.setCurrent_page(1);
//        resp.setToday_user_num(1000);
//        resp.setRegister_total_num(500);
//        resp.setToday_increment_num(100);
//        List<HotelCustomerBrief> customerBrief = new ArrayList<HotelCustomerBrief>();
//        HotelCustomerBrief scb1 = new HotelCustomerBrief();
//        scb1.setCreate_time(new Date().getTime());
//        scb1.setHistory_operations_times(1132);
//        scb1.setHouse_num(124);
//        scb1.setLast_arrive_hotel("全季14ff酒店");
//        scb1.setLast_arrive_time(new Date().getTime());
//        scb1.setMobile(123213232);
//        scb1.setUser_name("asdasdjsa");
//
//        HotelCustomerBrief scb2 = new HotelCustomerBrief();
//        scb2.setCreate_time(new Date().getTime());
//        scb2.setHistory_operations_times(2132);
//        scb2.setHouse_num(324);
//        scb2.setLast_arrive_hotel("全季24ff酒店");
//        scb2.setLast_arrive_time(new Date().getTime());
//        scb2.setMobile(123213232);
//        scb2.setUser_name("sdlkjsdlkjdsflk");
//        customerBrief.add(scb1);
//        customerBrief.add(scb2);
//
//        resp.setItem_list(customerBrief);
//
//        return JSON.toJSONString(resp);
//
//    }

    /**
     * 用户管理-用户列表-获取用户列表
     * 请求方式：POST
     *
     * @return
     */
    @RequestMapping(value = "/data/users",
            produces = {"application/json;charset=UTF-8"})
    public CompletableFuture<String> findUsers(@RequestBody JSONObject params) {
        LOGGER.info("用户管理-用户列表-获取用户列表->" + params);
        return CompletableFuture.supplyAsync(() -> {
            Integer page = params.getInteger("page");
            Integer size = params.getInteger("size");
            JSONObject search = params.getJSONObject("search");
            JSONObject resultJson = new JSONObject();
            resultJson.put("total_pages", 1);
            resultJson.put("total_count", 158);
            resultJson.put("current_page", 1);
            resultJson.put("size", 2);
            resultJson.put("register_total_num", 500);
            resultJson.put("today_user_num", 1000);
            resultJson.put("today_increment_num", 100);
            resultJson.put("today_active", 123);
            List<HotelCustomerInfo> item_list = new ArrayList();
            for (int i = 0; i < 100; i++) {
                HotelCustomerInfo hotelCustomerInfo1 = new HotelCustomerInfo();
                hotelCustomerInfo1.setId(1);
                hotelCustomerInfo1.setUser_name("欧阳靖" + i);
                hotelCustomerInfo1.setMobile("12345678901" + i);
                hotelCustomerInfo1.setCreate_time(new Date());
                hotelCustomerInfo1.setLast_arrive_time(new Date());
                hotelCustomerInfo1.setLast_arrive_hotel("全季上海酒店");
                hotelCustomerInfo1.setHouse_num(201 + i);
                hotelCustomerInfo1.setHistory_operations_times(59);
                item_list.add(hotelCustomerInfo1);
            }

            Paging pagination = Paging.pagination(item_list.size(), size, page);
            int fromIndex = pagination.getQueryIndex();
            int toIndex = 0;
            if (fromIndex + pagination.getPageSize() >= item_list.size()) {
                toIndex = item_list.size();
            } else {
                toIndex = fromIndex + pagination.getPageSize();
            }
            if (fromIndex > toIndex) {
                return CommonResult.jsonString(Collections.EMPTY_LIST);
            }
            if (search == null) {
                resultJson.put("item_list", item_list.subList(fromIndex, toIndex));
                return CommonResult.jsonString(resultJson);
            }
            if (!search.containsKey("content")) {
                resultJson.put("item_list", item_list.subList(fromIndex, toIndex));
                return CommonResult.jsonString(resultJson);
            }
            String content = search.getString("content");
            if (StringUtils.isEmpty(content)) {
                resultJson.put("item_list", item_list.subList(fromIndex, toIndex));
                return CommonResult.jsonString(resultJson);
            }
            List<HotelCustomerInfo> collect = item_list.stream().filter(e -> e.getUser_name().equals(content) || e.getMobile().equals(content)).collect(Collectors.toList());
            resultJson.put("item_list", collect);
            return CommonResult.jsonString(resultJson);
        });
    }

    /**
     * 用户管理-用户列表-用户详情操作日志
     * 请求方式：POST
     *
     * @return
     */
    @RequestMapping(value = "/data/users/{user_id}",
            produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    public CompletableFuture<String> findUserByUserId(@RequestBody JSONObject params, @PathVariable int user_id) {
        LOGGER.info("用户管理-用户列表-用户详情操作日志->" + params + user_id);
        return CompletableFuture.supplyAsync(() -> {
            Integer page = params.getInteger("page");
            Integer size = params.getInteger("size");
            List<JSONObject> list = new ArrayList<>();
            Calendar calendar = Calendar.getInstance();
            for (int i = 0; i < 100; i++) {
                JSONObject resultJson = new JSONObject();
                resultJson.put("total_pages", 10);
                resultJson.put("total_count", 100);
                resultJson.put("current_page", 1);
                resultJson.put("size", 10);
                resultJson.put("user_name", "欧阳靖");
                resultJson.put("mobile", "121212121");
                resultJson.put("user_id", i);
                resultJson.put("create_time", calendar.getTimeInMillis());
                resultJson.put("last_arrive_time", calendar.getTimeInMillis());
                resultJson.put("last_arrive_hotel", "全季上海酒店");
                resultJson.put("room_name", 201);
                resultJson.put("history_operations_times", 99);
                list.add(resultJson);
            }
            List<JSONObject> listCondition = list.stream().filter(e -> e.getInteger("user_id") == user_id).collect(Collectors.toList());
            JSONObject user = listCondition.get(0);
            List info = new ArrayList();
            for (int i = 0; i < 5; i++) {
                LogData logData = new LogData();
                logData.setTime(calendar.getTimeInMillis());
                List info1 = new ArrayList();
                info1.add("睡眠模式启动成功");
                logData.setInfo(info1);
                LogData logData1 = new LogData();
                logData1.setTime(calendar.getTimeInMillis());
                List info2 = new ArrayList();
                info2.add("门厅吸顶灯");
                info2.add("客厅吸顶灯");
                info2.add("阳台吸顶灯");
                logData1.setInfo(info2);
                LogBean logBean = new LogBean();
                List<LogData> logDataList = new ArrayList<>();
                logDataList.add(logData);
                logDataList.add(logData1);
                logBean.setTime(new Date());
                logBean.setData(logDataList);
                info.add(logBean);
            }
            user.put("info", info);

            return CommonResult.jsonString(user);
        });
    }


    /**
     * 用户管理-新增统计-获取新增统计
     * 请求方式：GET
     *
     * @return
     */
    @RequestMapping(value = "/data/users/statistics/increment",
            produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public CompletableFuture<String> findIncrement() {
        JSONArray resultArray = new JSONArray();
        JSONObject resultJson1 = new JSONObject();
        resultJson1.put("name", "今日活跃");
        resultJson1.put("data", 123);
        JSONObject resultJson2 = new JSONObject();
        resultJson2.put("name", "昨日活跃");
        resultJson2.put("data", 234);
        JSONObject resultJson3 = new JSONObject();
        resultJson3.put("name", "近30日活跃");
        resultJson3.put("data", 908);
        resultArray.add(resultJson1);
        resultArray.add(resultJson2);
        resultArray.add(resultJson3);
        return CompletableFuture.supplyAsync(() -> {
            return CommonResult.jsonString(resultArray);
        });
    }

    /**
     * 用户管理-新增统计-获取新增趋势统计
     * 请求方式：POST
     *
     * @return
     */
    @RequestMapping(value = "/data/users/statistics/increment",
            produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    public CompletableFuture<String> increment(@RequestBody JSONObject params) {
        LOGGER.info("用户管理-新增统计-获取新增趋势统计->" + params);
        return CompletableFuture.supplyAsync(() -> {
            JSONObject resultJson = new JSONObject();
            JSONArray seriesNames = new JSONArray();
            seriesNames.add("近30日个品类活跃和控制趋势");
            resultJson.put("seriesNames", seriesNames);


            JSONArray dataArray = new JSONArray();
            for (int i = 0; i < 10; i++) {
                StatisticsData statisticsData = new StatisticsData();
                statisticsData.setName("aaaaa");
                statisticsData.setValue(15551.40263680297);
                StatisticsData statisticsData1 = new StatisticsData();
                statisticsData1.setName("bbbbb");
                statisticsData1.setValue(5577.887235112961);
                List list = new ArrayList();
                list.add(statisticsData);
                list.add(statisticsData1);
                Tooltips tooltips = new Tooltips();
                tooltips.setData(list);
                tooltips.setUnit("单位");
                StatisticsBean statisticsBean = new StatisticsBean();
                statisticsBean.setValue(99.82998594154479);
                statisticsBean.setTooltips(tooltips);
                dataArray.add(statisticsBean);
            }
            JSONObject series = new JSONObject();
            series.put("data", dataArray);
            series.put("seriesName", "今日新增各酒店占比");
            resultJson.put("series", series);
            resultJson.put("unit", "y轴单位");
            resultJson.put("xAxis", new String[]{"全季1酒店",
                    "全季2酒店",
                    "全季3酒店",
                    "全季4酒店",
                    "全季5酒店",
                    "全季6酒店",
                    "全季7酒店"});

            return CommonResult.jsonString(resultJson);
        });
    }


    /**
     * 用户管理-活跃统计-获取活跃统计
     * 请求方式：GET
     *
     * @return
     */
    @RequestMapping(value = "/data/users/statistics/active",
            produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public CompletableFuture<String> active() {
        JSONArray resultArray = new JSONArray();
        JSONObject resultJson1 = new JSONObject();
        resultJson1.put("name", "今日活跃");
        resultJson1.put("data", 123);
        JSONObject resultJson2 = new JSONObject();
        resultJson2.put("name", "昨日活跃");
        resultJson2.put("data", 234);
        JSONObject resultJson3 = new JSONObject();
        resultJson3.put("name", "近30日活跃");
        resultJson3.put("data", 908);
        JSONObject resultJson4 = new JSONObject();
        resultJson4.put("name", "用户粘性");
        resultJson4.put("data", "4.4%");
        resultArray.add(resultJson1);
        resultArray.add(resultJson2);
        resultArray.add(resultJson3);
        resultArray.add(resultJson4);
        return CompletableFuture.supplyAsync(() -> {
            return CommonResult.jsonString(resultArray);
        });
    }

    /**
     * 用户管理-活跃统计-获取活跃趋势统计
     * 请求方式：POST
     *
     * @return
     */
    @RequestMapping(value = "/data/users/statistics/active",
            produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    public CompletableFuture<String> activePost(@RequestBody JSONObject params) {
        LOGGER.info("用户管理-活跃统计-获取活跃趋势统计->" + params);
        return CompletableFuture.supplyAsync(() -> {
            JSONObject resultJson = new JSONObject();
            JSONArray seriesNames = new JSONArray();
            seriesNames.add("近30日活跃趋势");
            StatisticsData statisticsData = new StatisticsData();
            statisticsData.setName("aaaaa");
            statisticsData.setValue(15551.40263680297);
            StatisticsData statisticsData1 = new StatisticsData();
            statisticsData1.setName("bbbbb");
            statisticsData1.setValue(5577.887235112961);
            List list = new ArrayList();
            list.add(statisticsData);
            list.add(statisticsData1);
            Tooltips tooltips = new Tooltips();
            tooltips.setData(list);
            tooltips.setUnit("单位");

            StatisticsBean statisticsBean = new StatisticsBean();
            statisticsBean.setValue(99.82998594154479);
            statisticsBean.setTooltips(tooltips);

            StatisticsBean statisticsBean1 = new StatisticsBean();
            statisticsBean1.setValue(99.80414896083639);
            statisticsBean1.setTooltips(null);

            StatisticsBean statisticsBean2 = new StatisticsBean();
            statisticsBean2.setValue(100.07159946025217);
            statisticsBean2.setTooltips(null);

            StatisticsBean statisticsBean3 = new StatisticsBean();
            statisticsBean3.setValue(100.20705737861071);
            statisticsBean3.setTooltips(null);

            StatisticsBean statisticsBean4 = new StatisticsBean();
            statisticsBean4.setValue(100.20005719240372);
            statisticsBean4.setTooltips(null);

            StatisticsBean statisticsBean5 = new StatisticsBean();
            statisticsBean5.setValue(100.19485210308152);
            statisticsBean5.setTooltips(null);

            JSONArray dataArray = new JSONArray();
            dataArray.add(statisticsBean);
            dataArray.add(statisticsBean1);
            dataArray.add(statisticsBean2);
            dataArray.add(statisticsBean3);
            dataArray.add(statisticsBean4);
            dataArray.add(statisticsBean5);

            JSONObject series = new JSONObject();
            series.put("data", dataArray);
            series.put("seriesName", "近30日活跃趋势");

            resultJson.put("seriesNames", seriesNames);
            resultJson.put("series", series);
            resultJson.put("unit", "y轴单位");
            resultJson.put("xAxis", new int[]{1,
                    2,
                    3,
                    4,
                    5,
                    6,
                    7});

            return CommonResult.jsonString(resultJson);
        });
    }

}
